package wnhz.com.business.changepwd;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import wnhz.com.business.LoginActivity;
import wnhz.com.business.MyApplication;
import wnhz.com.business.R;
import wnhz.com.business.base.MyBaseActivity;
import wnhz.com.business.bindphone.BindPhone4Activtity;
import wnhz.com.business.groupshop.bean.MyWalletBean;
import wnhz.com.business.groupshop.ui.FundActivity;
import wnhz.com.business.netcallback.DialogCallback;
import wnhz.com.business.shareshop.ShareShopActivity;
import wnhz.com.business.uitls.Base64Util;
import wnhz.com.business.uitls.ToastUtils;
import wnhz.com.business.uitls.Url;

/**
 * Created by lizhenglong on 2017-10-27 0027.
 * 修改密码_设置新密码
 * 5-交易密码  6-登陆密码
 */

public class ChangePwd3Activity extends MyBaseActivity {


    @BindView(R.id.ed_changepwd3_pwd)//新密码输入
    EditText edChangepwd3Pwd;
    @BindView(R.id.img_changepwd3_pwd)//新密码显示隐藏
    ImageView imgChangepwd3Pwd;
    @BindView(R.id.ed_changepwd3_pwd2)//确认密码输入
    EditText edChangepwd3Pwd2;
    @BindView(R.id.img_changepwd3_pwd2)//确认密码显示隐藏
    ImageView imgChangepwd3Pwd2;
    @BindView(R.id.btn_changepwd3)//提交
    Button btnChangepwd3;
    @BindView(R.id.tv_changepwd3_type)//设置密码类型 5-交易密码  6-登陆密码
    TextView tvChangepwd3Type;

    private String type = "";
    private String code = "";
    private String mobile = "";
    private boolean pwdflag1 = true;
    private boolean pwdflag2 = true;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chagepwd3);
        ButterKnife.bind(this);
        type = getIntent().getStringExtra("type");
        code = getIntent().getStringExtra("code");
        mobile = getIntent().getStringExtra("mobile");
        if (type.equals("5")){// type      1 注册  2 登录  3 忘记密码   4 更换手机号  5 修改交易密码  6 修改登录密码  7三方登陆  8绑定银行卡
            tvChangepwd3Type.setText("支付密码");
            edChangepwd3Pwd.setHint("请输入6位支付密码");
            edChangepwd3Pwd2.setHint("请输入6位支付密码");
        }else if (type.equals("6")){
            tvChangepwd3Type.setText("登录密码");
            edChangepwd3Pwd.setHint("请输入登录密码");
            edChangepwd3Pwd2.setHint("请输入登录密码");
        }
    }

    @OnClick({R.id.rl_register_back,R.id.img_changepwd3_pwd,R.id.img_changepwd3_pwd2,R.id.btn_changepwd3})
    void Click(View v){
        switch (v.getId()){
            case R.id.rl_register_back://返回
                finish();
                break;
            case R.id.img_changepwd3_pwd://新密码显示隐藏
                if (pwdflag1) {
                    pwdflag1 = false;
                    imgChangepwd3Pwd.setImageResource(R.drawable.ic_login_show);//设置显示密码的图片
                    edChangepwd3Pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    edChangepwd3Pwd.setSelection(edChangepwd3Pwd.getText().toString().length()); //调整光标的位置到最后
                } else {
                    pwdflag1 = true;
                    imgChangepwd3Pwd.setImageResource(R.drawable.ic_login_notshow);//设置隐藏密码的图片
                    edChangepwd3Pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    edChangepwd3Pwd.setSelection(edChangepwd3Pwd.getText().toString().length());
                }
                break;
            case R.id.img_changepwd3_pwd2://确认密码显示隐藏
                if (pwdflag2) {
                    pwdflag2 = false;
                    imgChangepwd3Pwd2.setImageResource(R.drawable.ic_login_show);//设置显示密码的图片
                    edChangepwd3Pwd2.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    edChangepwd3Pwd2.setSelection(edChangepwd3Pwd2.getText().toString().length()); //调整光标的位置到最后
                } else {
                    pwdflag2 = true;
                    imgChangepwd3Pwd2.setImageResource(R.drawable.ic_login_notshow);//设置隐藏密码的图片
                    edChangepwd3Pwd2.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    edChangepwd3Pwd2.setSelection(edChangepwd3Pwd2.getText().toString().length());
                }
                break;
            case R.id.btn_changepwd3://提交
                if (TextUtils.isEmpty(type)){
                    MyToast("获取设置密码类型失败");
                    return;
                }
                if (TextUtils.isEmpty("code")){
                    MyToast("获取验证码失败");
                    return;
                }
                if (TextUtils.isEmpty(edChangepwd3Pwd.getText().toString())){
                    MyToast("请填写密码");
                    return;
                }
                if (TextUtils.isEmpty(edChangepwd3Pwd2.getText().toString())){
                    MyToast("请填写密码");
                    return;
                }
                if (!edChangepwd3Pwd.getText().toString().equals(edChangepwd3Pwd2.getText().toString())){
                    MyToast("密码输入不一致");
                    return;
                }
                if(type.equals("5")){
                    if (edChangepwd3Pwd.getText().toString().length()!=6){
                        MyToast("支付密码长度为6位");
                    }
                }
                if (edChangepwd3Pwd.getText().toString().length()<6){
                    MyToast("密码长度过短");
                }
                ChangeNewPwd();
                break;
        }
    }

    private void ChangeNewPwd() {
        Map<String, String> map = new HashMap<>();
        if (MyApplication.getInstance().getInfoBean() == null) {
            MyApplication.getInstance().BackLoginActivity();
            return;
        }
        map.put("token", MyApplication.getToken());
        if (!TextUtils.isEmpty(mobile)){
            map.put("mobile",Base64Util.encodedString2(mobile));
        }
        map.put("type",type);
        map.put("code",code);
        map.put("password",Base64Util.encodedString2(edChangepwd3Pwd.getText().toString()));
        map.put("repassword",Base64Util.encodedString2(edChangepwd3Pwd2.getText().toString()));
        OkGo.<String>post(type.equals("3")?Url.USER_FORGOTPASSWORD:Url.UCENTER_CHANGEPASS)
                .tag(this)
                .params(map, false)
                .execute(new DialogCallback<String>(this) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Log.e("设置新密码onSuccess", response.body().toString());
                        JSONObject jsonObject = null;
                        try {
                            jsonObject = new JSONObject(response.body().toString());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        String re = jsonObject.optString("re");
                        if (re.equals("1")) {
                            ToastUtils.showToast(ChangePwd3Activity.this,jsonObject.optString("info"));
                            if (!type.equals("3")) {
                                startActivity(ShareShopActivity.createIntent(ChangePwd3Activity.this, 3));//跳转到主页指定fragment
                            }else {
                                startActivity(new Intent(ChangePwd3Activity.this,LoginActivity.class));
                            }
                            finish();
                        } else {
                            ToastUtils.showToast(ChangePwd3Activity.this, jsonObject.optString("info"));
                        }
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                    }
                });
    }

    @Override
    protected void handleMsg(Message msg) {

    }
}
